package 算法.leetcode.offer;


import 算法.leetcode.offer.base.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author lchenglong
 * @date 2022/4/27
 */
public class Offer32_I {

    public static void main(String[] args) {

    }
    public int[] levelOrder(TreeNode root) {
        // 边界条件
        if (root==null)return new int[0];
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        List<Integer> ans = new ArrayList<>();
        while (queue.size()>0){
            int size = queue.size();
            for (int i =0;i<size;i++) {
                TreeNode node = queue.poll();
                if (node.left!=null)queue.add(node.left);
                if (node.right!=null)queue.add(node.right);
                ans.add(node.val);
            }
        }
        return ans.stream().mapToInt(Integer::valueOf).toArray();
    }
}
